package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.query.PrivateMessageQueryObject;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author by mycp
 * @Classname PrivateMessageController
 * @Description TODO 私信上传控制处理
 * @Date 2018/11/2 11:36
 */
@RestController
@RequestMapping("/privateMessages")
public class PrivateMessageController {

    @Autowired
    private IPrivateMessageService privateMessageService;

    /**
     * 根据登录者的id和作者的id查询所有对话
     * 接口设计:
     * 资源:/privateMessages
     * 参数:
     * 动作: GET
     */
    @GetMapping
    public Object getConversations(PrivateMessageQueryObject qo) {
        qo.setOrderBy("sendTime desc");
        qo.setPageSize(10);
        PageInfo pageInfo = privateMessageService.getMessagesByUserIdAndFriendId(qo);
        return pageInfo;
    }

    /**
     * 根据登录者的id查询他所有的对话者,以及对应对话信息的未读数量
     * 接口设计:
     * 资源:/privateMessages
     * 参数:
     * 动作: GET
     */
    @GetMapping("/{userId}")
    public Object getFriends(PrivateMessageQueryObject qo) {
        qo.setPageSize(0);
        List<Map> list = privateMessageService.getFriendIdAndUnReadCount(qo);
        return list;
    }

    /**
     * 根据对话双方的id更改消息的状态
     * 接口设计:
     * 资源:/privateMessages
     * 参数:
     * 动作: PUT
     */
    @PutMapping("/{userId}")
    public Object updateStatus(@PathVariable Long userId, Long friendId, Integer status, Date sendTime) {
        String format = null;
        if (sendTime != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            format = sdf.format(sendTime);
        }
        privateMessageService.updateStatus(userId,friendId,status,format);
        return new JsonResult();
    }

    /**
     * 根据登录者的id查询他所有的未读消息的数量
     * 接口设计:
     * 资源:/privateMessages
     * 参数:
     * 动作: GET
     */
    @GetMapping("/{userId}/counts")
    public Object getUnRead(@PathVariable Long userId,Integer status) {
        Integer count = privateMessageService.getUnRead(userId ,status);
        return count;
    }

}
